package cn.tedu.ivoswork.dict.service.impl;

import cn.tedu.ivoswork.dict.mapper.DictMapper;
import cn.tedu.ivoswork.dict.pojo.dto.DictQuery;
import cn.tedu.ivoswork.dict.pojo.dto.SaveDictFormParam;
import cn.tedu.ivoswork.dict.pojo.entity.Dict;
import cn.tedu.ivoswork.dict.pojo.vo.DictVO;
import cn.tedu.ivoswork.dict.service.IDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictService implements IDictService {
    @Autowired
    DictMapper dictMapper;

    @Override
    public List<DictVO> select(DictQuery dictQuery) {
        log.debug("查询字典业务：参数={}",dictQuery);

        return dictMapper.select(dictQuery);
    }

    @Override
    public void save(SaveDictFormParam saveDictFormParam) {
        log.debug("保存字典业务：参数={}",saveDictFormParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(saveDictFormParam,dict);
        if(dict.getId()!=null){
            log.debug("更新字典业务：参数={}",dict);
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }else{
            log.debug("新增字典业务：参数={}",dict);
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("删除字典业务：参数={}",id);
        dictMapper.delete(id);
    }
}
